package com.example.culturapp;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class NotificationService extends IntentService {
	private String nombre;
	private String lugar;
	private String fecha;
	private String descripcion;
	private String hora;
	private int imagen;
	private boolean isBitmap;
	private byte[] byteArray;
	
	public NotificationService() {
		super("NotificationService");;
	}

	public NotificationService(String name) {
		super(name);
	}
	
	@Override
	protected void onHandleIntent(Intent arg0) {
		int mNotificationId = 001;
		// Gets an instance of the NotificationManager service
		NotificationManager mNotifyMgr = 
		        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		// Builds the notification and issues it.
		mNotifyMgr.notify(mNotificationId, getNotificationBuilder().build());
	}

    public NotificationCompat.Builder getNotificationBuilder() {
		Intent resultIntent = new Intent(this, MostrarEventoActivity.class);
		resultIntent.putExtra("nombre",nombre);
		resultIntent.putExtra("lugar",lugar);
		resultIntent.putExtra("fecha",fecha);
		resultIntent.putExtra("descripcion",descripcion);
		resultIntent.putExtra("hora",hora);
		resultIntent.putExtra("isBitmap",isBitmap);
       	if(!isBitmap){
       		resultIntent.putExtra("imagen",imagen);
       	}else{
       		resultIntent.putExtra("bitmap", byteArray);
       		//resultIntent.putExtra("bitmap",(Bitmap)item.getImagen());
       	}
		// Because clicking the notification opens a new ("special") activity, there's
		// no need to create an artificial back stack.
		PendingIntent resultPendingIntent =
		    PendingIntent.getActivity(
		    this,
		    0,
		    resultIntent,
		    PendingIntent.FLAG_UPDATE_CURRENT
		);
		
		NotificationCompat.Builder mBuilder =
			    new NotificationCompat.Builder(this)
			    .setSmallIcon(R.drawable.ico)
			    .setContentTitle("CulturApp")
			    .setContentText("Hay novedades de sus artistas favoritos");
		
		mBuilder.setContentIntent(resultPendingIntent);
		SystemClock.sleep(7000);
		return mBuilder;
	}
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    	if(intent!=null){
    		nombre = intent.getStringExtra("nombre");
			lugar = intent.getStringExtra("lugar");
			fecha = intent.getStringExtra("fecha");
			descripcion = intent.getStringExtra("descripcion");
			hora = intent.getStringExtra("hora");
			isBitmap = intent.getBooleanExtra("isBitmap", false);
			if(!isBitmap){
				imagen = intent.getIntExtra("imagen", R.drawable.abel_pintos);
				//item=new ItemHoy(nombre, imagen, descripcion, fecha, hora, lugar);
			}else{
				//item=new ItemHoy(nombre, bitmap, descripcion, fecha, hora, lugar);
				byteArray = intent.getByteArrayExtra("bitmap");
			}
    	}
    	return super.onStartCommand(intent, flags, startId);
    }
}
